package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.aftersales.bean.*;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShCustomerInfoMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShMachineMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-28
 */
@Service
public class ShRepairUpkeepServiceImpl extends ServiceImpl<ShRepairUpkeepMapper, ShRepairUpkeep> implements ShRepairUpkeepService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    ShMachineMapper shMachineMapper;
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;

    @Override
    public boolean insertShRepairUpkeep(ShRepairUpkeep shRepairUpkeep) {
        Wrapper<ShRepairUpkeep> wrapper =new EntityWrapper<>();
        String  code=new SimpleDateFormat("yyyyMMdd").format(new Date());
        wrapper.like("code",code, SqlLike.RIGHT);
        wrapper.orderBy("code",false);
        wrapper.last(" limit 1 ");
        List<ShRepairUpkeep> shRepairUpkeeps = super.selectList(wrapper);
        if (shRepairUpkeeps!=null&&shRepairUpkeeps.size()>0){
            ShRepairUpkeep shRepairUpkeep1 = shRepairUpkeeps.get(0);
               code=  String.valueOf(Long.valueOf( shRepairUpkeep1.getCode()) +1) ;
        }else {
            code=code+"01";
        }
        shRepairUpkeep.setCode(code);
        shRepairUpkeep.setState(0);
        shRepairUpkeep.setIsStart(0);
        boolean insert = super.insert(shRepairUpkeep);
        return insert;
    }

    @Override
    public boolean updateShRepairUpkeep(ShRepairUpkeep shRepairUpkeep, Token token) {
        ShRepairUpkeep oldShrepairUpkeep = super.selectById(shRepairUpkeep.getId());
            if (shRepairUpkeep.getIsStart()==1){
                if (oldShrepairUpkeep.getIsStart()==0){
                    shRepairUpkeep.setStartDate(new Date());
                    shRepairUpkeep.setState(1);
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeep.getResult(),null)){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(oldShrepairUpkeep.getResult(),null)){
                    shRepairUpkeep.setState(2);
                }
            }

        boolean b = super.updateById(shRepairUpkeep);
        return b;
    }

    @Override
    public Page<ShRepairUpkeep> selectShRepairUpkeep(Page<ShRepairUpkeep> page, QueryShRepairUpkeep queryShRepairUpkeep) {
        Wrapper<ShRepairUpkeep> wrapper=new EntityWrapper<>();
        wrapper.eq("state",queryShRepairUpkeep.getState());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShRepairUpkeep.getCode(),null)){
            wrapper.like("code",queryShRepairUpkeep.getCode());
        }
      page=  super.selectPage(page,wrapper);
        List<ShRepairUpkeep> records = page.getRecords();
        for (ShRepairUpkeep shRepairUpkeep : records) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeep.getMachine(),null)){
                ShMachine shMachine = shMachineMapper.selectById(shRepairUpkeep.getMachine());
                shRepairUpkeep.setMachineName(shMachine);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeep.getCustomer(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(shRepairUpkeep.getCustomer());
                shRepairUpkeep.setCustomerName(shCustomerInfo);

            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeep.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairUpkeep.getCreator());
                shRepairUpkeep.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairUpkeep.getRepairer(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairUpkeep.getRepairer());
                shRepairUpkeep.setRepairerName(jyUser.getName());
            }

        }
        page.setTotal(super.selectCount(wrapper));

        return page;
    }

    @Override
    public List<JyUser> selectShRepairUpkeepPersons() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("220");
        return jyUsers;
    }
}
